using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Enume;
using SO;
using Spine;
using Struct;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Serialization;

namespace Entity {

    public partial class EntityController {


        public List<FightTriggerItemData> fightTreasureItemDatas = new List<FightTriggerItemData>();


        /// <summary>
        /// 添加宝物
        /// </summary>
        /// <param name="treasureId"></param>
        /// <param name="level"></param>
        public void AddFightTreasure(int treasureId, int level = 1) {
            TreasureItemConfigSO configSo = TreasureManager.Instance.GetConfigById(treasureId);
            if (configSo.itemCheckTiming == EnumItemCheckTiming.AfterLearn) {
                List<PropertiesStruct> propertiesStructs = configSo.itemAdditionalAttributes;
                if (propertiesStructs.IsNullOrEmpty()) {
                    return;
                }
                propertiesStructs.ForEach(e => { GameLayer.Instance.PlayerController.FightProperties.AddProperty(e.propertiesType, e.value, e.isPercentage); });
            }

           
            FightTriggerItemData firstSkillData = fightTreasureItemDatas.FirstOrDefault(x => x.id == treasureId);
            if (configSo.itemCheckTiming == EnumItemCheckTiming.TriggeredByCode) {
                CheckFightTreasureTriggeredByCode(configSo,firstSkillData);
            }

            if (firstSkillData != null) {
                firstSkillData.level += level;
                if (firstSkillData.level < 0) {
                    firstSkillData.level = 0;
                }
            } else {
                fightTreasureItemDatas.Add(new FightTriggerItemData(treasureId, level));
            }
        }

        private void CheckFightTreasureTriggeredByCode(TreasureItemConfigSO itemConfigSo, FightTriggerItemData firstSkillData) {

            //多种选择 升级选择技能的时候选择的栏目从3提升为4
            if (itemConfigSo.itemId == 13000) {
                FightProperties.selectSkillPanelCount = 4;
            }
            //13001 选择技能的时候刷新次数+1
            if (itemConfigSo.itemId == 13001) {
                FightProperties.selectSkillPanelMaxRefreshCount += 1;
            }

            
        }

        /// <summary>
        /// 获取宝物等级
        /// </summary>
        /// <param name="treasureId"></param>
        /// <returns></returns>
        public int GetTreasureLevel(int treasureId) {
            FightTriggerItemData firstSkillData = fightTreasureItemDatas.FirstOrDefault(x => x.id == treasureId);
            if (firstSkillData != null) {
                return firstSkillData.level;
            } else {
                return 0;
            }
        }

        /// <summary>
        /// 根据宝物检查时机获取宝物数据
        /// </summary>
        /// <param name="itemCheckTiming"></param>
        /// <returns></returns>
        public List<(FightTriggerItemData, BaseTriggerItemSO)> GetFightTreasuresBySkillCheckTiming(EnumItemCheckTiming itemCheckTiming) {
            List<(FightTriggerItemData, BaseTriggerItemSO)> skillDatas = new();
            foreach (FightTriggerItemData skillData in fightTreasureItemDatas) {
                TreasureItemConfigSO itemConfigSo = TreasureManager.Instance.GetConfigById(skillData.id);
                if (itemConfigSo.itemCheckTiming == itemCheckTiming) {
                    skillDatas.Add((skillData, itemConfigSo));
                }
            }

            return skillDatas;
        }

        /// <summary>
        /// 移除宝物
        /// </summary>
        /// <param name="treasureId"></param>
        public void RemoveFightTreasure(int treasureId) {
            FightTriggerItemData firstSkillData = fightTreasureItemDatas.FirstOrDefault(x => x.id == treasureId);
            if (firstSkillData != null) {
                fightTreasureItemDatas.Remove(firstSkillData);
            }
        }

        /// <summary>
        /// 清空宝物
        /// </summary>
        public void ClearFightTreasures() {
            fightTreasureItemDatas.Clear();
        }

        /// <summary>
        /// 宝物是否存在
        /// </summary>
        /// <param name="treasureId"></param>
        /// <returns></returns>
        public bool TreasureIsExist(int treasureId, out int skillLevel) {
            skillLevel = GetTreasureLevel(treasureId);
            return skillLevel > 0;
        }



     
    }

}